import { Component, OnInit, Input } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { RoleService } from 'src/app/services/role.service';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
@Component({
  selector: 'zepride-jurisdiction-modal',
  templateUrl: './jurisdiction-modal.component.html',
  styleUrls: ['./jurisdiction-modal.component.less']
})
export class JurisdictionModalComponent implements OnInit {
  @Input() permitssList;
  @Input() accountId = '';
  AccountPermitsList = []; //添加人员权限
  constructor(
    private modalRef: NzModalRef<JurisdictionModalComponent>,
    private msg: NzMessageService,
    private roleService: RoleService
  ) { }

  ngOnInit(): void {
  }
  cancel() {
    this.modalRef.close();
  }
  checkAllLog(permitssList) {
    permitssList.value.forEach(e => {
      e.checked = permitssList.checked;
    });
  }
  confirm() {
    this.modalRef.close(true);
  }
  isCheckAll(permitssList) {
    permitssList.checked = permitssList.value.every(v => v.checked);
  }
  checkboxLog(item, permitssList): void {
    if (!item.checked) {
      permitssList.checked = false;
    } else {
      this.isCheckAll(permitssList);
    }
  }
  //添加人员权限
  async upjurisdiction() {
    //清空值
    this.AccountPermitsList = [];
    // push选中得数据
    this.permitssList.forEach(element => {
      element.value.forEach(element2 => {
        if (element2.checked) {
          this.AccountPermitsList.push({
            permitId: element2.permitId,
            permitName: element2.permitName,
            authObjectId: this.accountId,
            authObjectType: 'User'
          });
        }
      });
    });
    if (!this.AccountPermitsList.length) {
      this.msg.create('error', '请选择人员人员权限');
      return;
    }
    try {
      await this.roleService.createAccountPermits(this.AccountPermitsList);
      this.msg.success('添加人员权限成功');
      this.confirm();
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }
}
